<?php
namespace App\Http\Controllers\ApiApp\Reward;

use App\DBs\Reward\ScanDB;
use Illuminate\Http\Request;

class ScanController extends BaseController
{
    /**
     * 扫码
     */

    protected $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = new ScanDB();
    }

    /**
     * 列表
     */
    public function getIndex(Request $request)
    {
        //接口权限值
        $access = $request->post('appAccess','');
        if (env('APP_ENV')!='dev' &&
            $access!=$this->db->getAccessLast()) {
            return $this->toAppJson(-1);
        }
        //获取参数
        $uid = $request->post('uid',0);
        //梳理数据
        $resDb = $this->db->getIndex($uid);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-5);
        }
        return $this->toAppJson(0,$resDb['data']);
    }

    /**
     * 新增
     */
    public function setAdd(Request $request)
    {
        //接口权限值
        $access = $request->post('appAccess','');
        if (env('APP_ENV')!='dev' &&
            $access!=$this->db->getAccessLast()) {
            return $this->toAppJson(-1);
        }
        $uid = $request->post('uid',0);
        $type = $request->post('type',0);
        $link = $request->post('link','');
        if (!$uid || !$type || !$link) {
            return $this->toAppJson(-2);
        }
        //看看是否已有数据
        $resDb2 = $this->db->getOne($uid);
        if ($resDb2['code']==0) {
            return $this->toAppJson(-21);
        }
        //获取用户名称
        $resUser = $this->db->getUnameByUid($uid);
        //更新数据
        $resDb = $this->db->setAdd($uid,$resUser,$type,$link);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-6);
        }
        $dataArr = $resDb['data'];
        return $this->toAppJson(0, $dataArr);
    }
}